package com.aaa.controller;

import com.aaa.entity.Menu;
import com.aaa.entity.MyReturn;
import com.aaa.service.MenuService;

import com.aaa.util.TreeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author ：dudu（12232347710@qq.com）
 * @date ：Created in 2022/6/29 14:30
 * @description：
 * @modified By：
 */
@Controller
@RequestMapping("menu")
public class MenuController extends BaseController{
    @Autowired
    private MenuService menuService;
    MyReturn myReturn=new MyReturn();
    int num=0;
    @RequestMapping("menuUser.do")
    public String Page(){
        return "menuUser";
    }
    /**
     * @author     ：dudu
     * @Description: 查询所有菜单
    */
    @RequestMapping("findAllMenu.do")
    @ResponseBody
    public MyReturn findAllMenu() {
        List<Menu> allMenu = menuService.findAllMenu();
        myReturn.setData(allMenu);
        return super.getSuccessResult(myReturn);
    }
    /**
     * @author     ：dudu
     * @Description:查询所有菜单，用于添加和编辑菜单时的生成树
    */
    @RequestMapping("findAllMenus.do")
    @ResponseBody
    public List findAllMenus() {
        List<Menu> allMenu = menuService.findAllMenu();
        return TreeUtil.fromMenuListToTreeList(allMenu);
    }
    /**
     * @author     ：dudu
     * @Description:根据id查找父亲的id和名字
    */
    @RequestMapping("findParentNameId.do")
    @ResponseBody
    public MyReturn findParentNameId(Integer menuId) {
        Menu parent = menuService.findParentNameId(menuId);
        myReturn.setData(parent);
        return super.getSuccessResult(myReturn);
    }
    /**
     * @author     ：dudu
     * @Description: 增加菜单
    */
    @RequestMapping("addMenu.do")
    @ResponseBody
    public MyReturn addAllMenu(Menu menu) {
        if (menu.getParentId() == null) {
            menu.setParentId(0);
        }
        Menu insert = menuService.addMenu(menu);
        if (insert != null) {
            return super.getSuccessResult(myReturn);
        }
            return super.getFailResult(myReturn);
    }
    /**
     * @author     ：dudu
     * @Description: 删除
    */
    @RequestMapping("deleteMenu.do")
    @ResponseBody
    public MyReturn deleteMenu(Integer menuId) {
        boolean deleteMenu = menuService.deleteMenu(menuId);
        if (deleteMenu){
            return super.getSuccessResult(myReturn);
        }else {
            return super.getFailResult(myReturn);
        }
    }
   /**
    * @author     ：dudu
    * @Description: 假删除，隐藏菜单 数据库中还存在
   */

    @RequestMapping("deleteMenuById.do")
    @ResponseBody
    public MyReturn deleteMenuById(Integer menuId) {
        num = menuService.deleteMenuById(menuId);
        if (num > 0) {
            return super.getSuccessResult(myReturn);
        } else {
            return super.getFailResult(myReturn);
        }
    }
    @RequestMapping("updateMenu.do")
    @ResponseBody
    public MyReturn updateMenu(Menu menu) {
        try {
            Menu update = menuService.updateMenu(menu);
            myReturn.setData(update);
            return super.getSuccessResult(myReturn);
        } catch (Exception e) {
            return super.getFailResult(myReturn);
        }
    }
}
